package io.itit.ecp.admin.provide.selected;

import com.alibaba.fastjson.JSONObject;
import io.itit.ecp.admin.common.enums.IMeterDeviceEnums;
import io.itit.ecp.admin.provide.action.req.MeterDeviceReq;
import io.itit.ecp.admin.provide.action.resp.MeterDeviceResp;
import io.itit.ecp.admin.server.entity.EnergyConsumptionTypeEntity;
import io.itit.ecp.admin.server.entity.MeterDeviceEntity;
import io.itit.ecp.admin.server.service.IEnergyConsumptionTypeService;
import io.itit.ecp.admin.server.service.IMeterDeviceService;
import io.itit.grass.common.provide.action.resp.SelectedResp;
import io.itit.grass.common.server.selected.ISelected;
import io.itit.grass.common.server.utils.ConvertUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 计量设备选择器
 */
@Component
@RequiredArgsConstructor
public class MeterDeviceSelected implements ISelected<SelectedResp> {

    public static final String CODE = "MeterDeviceSelected";

    private final IMeterDeviceService meterDeviceService;

    private final IEnergyConsumptionTypeService energyConsumptionTypeService;

    @Override
    public String code() {
        return CODE;
    }

    @Override
    public List<SelectedResp> get(String param) {
        MeterDeviceReq req = JSONObject.isValidObject(param) ? JSONObject.parseObject(param, MeterDeviceReq.class) : new MeterDeviceReq();
        List<MeterDeviceEntity> meterDeviceEntities = meterDeviceService
                .lambdaQuery()
                .eq(StringUtils.isNotEmpty(req.getSceneProjectId()), MeterDeviceEntity::getSceneProjectId, req.getSceneProjectId())
                .eq(StringUtils.isNotEmpty(req.getSceneEdificeId()), MeterDeviceEntity::getSceneEdificeId, req.getSceneEdificeId())
                .eq(StringUtils.isNotEmpty(req.getSceneFloorId()), MeterDeviceEntity::getSceneFloorId, req.getSceneFloorId())
                .eq(StringUtils.isNotEmpty(req.getSceneRoomId()), MeterDeviceEntity::getSceneRoomId, req.getSceneRoomId())
                .eq(MeterDeviceEntity::getDelFlag, IMeterDeviceEnums.DelFlag.delFlag1.delFlag)
                .orderByAsc(MeterDeviceEntity::getDeviceCode)
                .list();
        Set<String> energyTypeIds = meterDeviceEntities.stream().map(MeterDeviceEntity::getEnergyTypeId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<String, EnergyConsumptionTypeEntity> energyConsumptionTypeEntityMap = energyTypeIds.isEmpty() ? Collections.emptyMap() :
                energyConsumptionTypeService.listByIds(energyTypeIds)
                        .stream()
                        .collect(Collectors.toMap(EnergyConsumptionTypeEntity::getId, Function.identity()));
        return meterDeviceEntities
                .stream().map(item -> {
                    EnergyConsumptionTypeEntity consumptionTypeEntity = energyConsumptionTypeEntityMap.get(item.getEnergyTypeId());
                    MeterDeviceResp deviceResp = ConvertUtils.convert(item, MeterDeviceResp.class);
                    if(Objects.nonNull(consumptionTypeEntity)){
                        deviceResp.setEnergyTypeName(consumptionTypeEntity.getEnergyName());
                        deviceResp.setEnergyTypeUnit(consumptionTypeEntity.getEnergyUnit());
                    }
                    return SelectedResp.builder().label(item.getDeviceName() + "（" + item.getDeviceCode() + "）").value(item.getId())
                            .baseObj(deviceResp)
                            .build();
                }).collect(Collectors.toList());
    }
}
